import { getLogout, getInfo } from '@/api/api'
import { deleteToken } from '@/utils/auth'

const userStore= {
    actions: {
        //请求用户信息
        async saveUserInfo ({ commit}) {
            const res =await getInfo();
            if (res.code === 200 || res.code === 20000){
                commit('SAVE_USER_INFO', res.data)
                console.log('获取用户信息成功')
            } else {
                console.log("获取用户信息失败")
            }
        },
        //退出登录
        async clearUserInfo ({ commit }) {
            const res = await getLogout();
            if (res.code === 200 || res.code === 20000) {
                deleteToken()
                commit('CLEAR_USER_INFO')
                console.log("退出登录成功")
            } else {
                return Promise.reject(new Error(`退出登录失败，${res.message}`))
            }
        }
    },
    state: {
        userInfo: {},
    },
    mutations: {
        SAVE_USER_INFO (state, userInfo) {
            state.userInfo = userInfo
        },
        CLEAR_USER_INFO (state) {
            state.userInfo = ''
        }
    }
}
export default userStore